﻿
$(function () {
    //初始化表格数据
    initTable(_global_web_app_path+'/system/user/page', [[
                { field: 'org_code', hidden: true },
                { field: 'role_codes', hidden: true },
                { field: 'org_name', title: '所属部门', width: 180, sortable: true },
                { field: 'user_code', title: '操作员编码', width: 80, sortable: true },
                { field: 'user_name', title: '姓名', width: 150, sortable: true },
                { field: 'roles', title: '角色', width: 100, sortable: true },
                { field: 'phone', title: '联系电话', width: 120 },
                { field: 'mobile_phone', title: '手机', width: 120 },
                { field: 'email', title: 'email', width: 120 },
                { field: 'order_id', title: '排序编码', width: 80 },
                { field: 'status', title: '状态', width: 80,formatter:formatStatus },
                { field: 'memo', title: '备注', width: 180 }
                ]]);

    //工具条按钮的事件注册
    $('#btnSearch').click(function () { searchFun(); });
    $('#btnAdd').click(function () { addFun(); });
    $('#btnEdit').click(function () { editFun(); });
    $('#btnRemove').click(function () { deleteFun(); });

    //初始化弹出框
    dialog=initDialog('dlg', saveFun);

    //绑定部门信息
    init_combobox_tree($("#Data_DepartmentId"), _global_web_app_path+"/system/org_info/orgtree");
    //绑定角色信息
    init_combobox_with_checkboxstyle($("#Data_role"), _global_web_app_path+"/system/user/role_list");

});

function formatStatus(val,row){
	if (val==1){
		return "启用";
	}else{
		return "停用";
	}
}

//查询数据
function searchFun() {
    dataTable.datagrid('reload', {
    	userCode: $('#Query_OperatorId').val(),
    	userName: $('#Query_OperatorName').val()
    });
}

//添加数据
function addFun() {
    operType = "Add";
    clearText();
    dialog.dialog('open').dialog('setTitle', '新增用户信息');
    $('#Data_OperatorId').focus().removeAttr('readonly');
}

//清空数据
function clearText() {
    $("#dlg").find("input[type=text]").val("");
    $("#dlg").find("input[type=password]").val("");
    $('#Data_role').combobox('clear');
    $("#Data_Status").find("option[value=启用]").attr("selected", true);
}

//编辑数据
function editFun() {
    operType = "Edit";
    clearText();
    //获取要编辑的行
    selRow = $('#dg').datagrid('getSelected');
    if (!selRow) {
        $.messager.alert('提示', '请选择需要编辑的数据信息', 'error');
        return;
    }
    
//    $('#Form1').form('load',{
//    	userCode:selRow.user_code
//	});
    
    $('#Data_OperatorId').val(selRow.user_code).attr('readonly', 'readonly');
    $('#Data_OperatorName').val(selRow.user_name);
    $('#Data_DepartmentId').combotree('clear');
    $('#Data_DepartmentId').combotree('setValue', selRow.org_code);
    if (selRow.roles){
        $('#Data_role').combobox('setValues', selRow.role_codes);
    }
    $('#Data_ContactNumber').val(selRow.phone);
    $('#Data_SortId').numberbox('setValue',selRow.order_id);
    $("#Data_Status").find("option[value=" + selRow.status + "]").attr("selected", true);

    dialog.dialog('open').dialog('setTitle', '修改用户信息');
}

//删除数据
function deleteFun() {
    //获取要删除的行
    selRow = $('#dg').datagrid('getSelected');
    if (!selRow) {
        $.messager.alert('提示', '请选择要删除的用户信息', 'error');
        return;
    }
    //删除之前给出确认提示
    $.messager.confirm('提示', '确定要删除姓名为[' + selRow.user_name + ']的用户信息吗？', function (r) {
        if (r) {
            $.ajax({
                type: "post",
                dataType: "json",
                url: _global_web_app_path+"/system/user/delete",
                data: "userCode=" + selRow.user_code.toString(),
                error: function (e) {
                    $.messager.alert('错误', e.responseText, 'error');
                },
                success: function (data) {
                    $.messager.alert('提示', data.msg);
                    dataTable.datagrid("reload");
                }
            });
        }
    });
}

//保存数据
function saveFun() {
	var valid_status=$('#Form1').form('validate');
    if (true==valid_status) {
        //构造要传递的参数
        var postData = {
        	userCode: $("#Data_OperatorId").val(),
        	userName: $("#Data_OperatorName").val(),
        	orgCode: $('#Data_DepartmentId').combobox('getValue'),
            phone: $('#Data_ContactNumber').val(),
            loginPass: $('#Data_Pwd').val(),
            orderId: $("#Data_SortId").val(),
            status: $("#Data_Status").val(),
            roles: $('#Data_role').combobox('getValues'),
            operType: operType
        };
        //ajax调用
        $.ajax({
            type: "post",
            contentType:"application/json",
            dataType: "json",
            url: ("Add"==operType)? _global_web_app_path+"/system/user/create":_global_web_app_path+"/system/user/update",
            data: JSON.stringify(postData),
            error: function (e) {
                $.messager.alert('错误', e.responseText, 'error');
            },
            success: function (data) {
                $.messager.alert('提示', data.msg);
                dialog.dialog('close');
                dataTable.datagrid("reload");
            }
        });
    }
}